<?php

namespace app\api\controller\kuyou\applet;

use app\api\model\kuyou\applet\IncomeType as IncomeTypes;
/**
 * 收入类别接口
 */
class IncomeType extends Common
{
    protected $noNeedRight = 'getDetail';

    /**
     * 获取收入类别
     * @ApiMethod (POST)
     * @param int $page 页码
     * @param int $limit 每页条数
     * @param string $type_name 分类名称
     */
    public function index()
    {
        if ($this->request->isPost()) {
            $page = $this->request->post('page');
            $page = empty($page)?1:$page;
            $limit = $this->request->post('limit');
            $limit = empty($limit)?config('app_limit'):$limit;

            $name = $this->request->post('name');
            $where = [];
            if(!empty($name)){
                $where['name'] = ['like','%'.$name.'%'];
            }
            $status = $this->request->post('status');
            if ($status==-1){
                $where['status'] = 0;
            }else if(!empty($status)){
                $where['status'] = $status;
            }

            $result = IncomeTypes::getList($where,$page,$limit);

            if(isset($result['list']) && isset($result['count'])){
                $this->success('查询成功',$result,200);
            }else{
                $this->error('查询失败','',406);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }

    /**
     * 获取收入类别详情
     * @ApiMethod (POST)
     * @param int $id 收入类别ID
     */
    public function getdetail()
    {
        if ($this->request->isPost()) {
            $id = $this->request->post('id');
            $result = IncomeTypes::getDetail($id);
            if($result['status']==200){
                $this->success('查询成功',$result['msg'],200);
            }else{
                $this->error($result['msg'],'',$result['status']);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }

    /**
     * 添加收入类别
     * @ApiMethod (POST)
     * @param array $detail 收入类别数据
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $name = html_entity_decode($this->request->post('name'));
            $image = html_entity_decode($this->request->post('image'));
            $status = html_entity_decode($this->request->post('status'));
            $detail = [
                'name' => $name,
                'image' => $image,
                'status'    => $status,
            ];
            $ret = IncomeTypes::addDetail($detail);
            if($ret['status']==200){
                $this->success('添加成功',$ret['msg'],200);
            }else{
                $this->error($ret['msg'],'',$ret['status']);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }

    /**
     * 更新收入类别
     * @ApiMethod (POST)
     * @param array $detail 收入类别数据
     */
    public function edit()
    {
        if ($this->request->isPost()) {
            $id = html_entity_decode($this->request->post('id'));
            $name = html_entity_decode($this->request->post('name'));
            $image = html_entity_decode($this->request->post('image'));
            $status = html_entity_decode($this->request->post('status'));
            $detail = [
                'id'                    => $id,
                'name'    => $name,
                'image'   => $image,
                'status'                => $status,
            ];
            $ret = IncomeTypes::editDetail($detail);
            if($ret['status']==200){
                $this->success('更新成功','',200);
            }else{
                $this->error($ret['msg'],'',$ret['status']);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }

    /**
     * 删除收入类别
     * @ApiMethod (POST)
     * @param int $id 收入类别id
     */
    public function del()
    {
        if ($this->request->isPost()) {
            $id = $this->request->post('id');
            $ret = IncomeTypes::delDetail($id);
            if($ret['status']==200){
                $this->success('删除成功',$ret['msg'],200);
            }else{
                $this->error($ret['msg'],'',$ret['status']);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }

}
